package com.mingtu.guards.adapter;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.mingtu.guards.R;


public class ReportTypedAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;

    public ReportTypedAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_report_type, null);

            holder.layout = (LinearLayout) convertView.findViewById(R.id.layout);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.iv_img);
            holder.tvText1 = (TextView) convertView.findViewById(R.id.tv_text1);
            holder.tvText2 = (TextView) convertView.findViewById(R.id.tv_text2);

            holder.layout.setOnClickListener(this);
            holder.layout.setTag(position);

            ViewGroup.LayoutParams lp =  holder.ivImg.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行2个，间隙是10dp
            int mWidth = (screenWidth - SizeUtils.dp2px(10) * 3) / 2;
            int mHeight = mWidth;
            lp.width = mWidth;
            lp.height = mHeight;
            holder.ivImg.setLayoutParams(lp);

            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();
        }


        switch (position) {
            case 0:
                holder.ivImg.setBackgroundResource(R.mipmap.img_upload_type1);
                holder.tvText1.setText("正常巡护");
                holder.tvText2.setText("Normal patrol");

                break;
            case 1:
                holder.ivImg.setBackgroundResource(R.mipmap.img_upload_type2);
                holder.tvText1.setText("紧急事件");
                holder.tvText2.setText("Emergency");
                break;
            case 2:
                holder.ivImg.setBackgroundResource(R.mipmap.img_upload_type3);
                holder.tvText1.setText("异常行为");
                holder.tvText2.setText("Abnormal behavior");
                break;
            case 3:
                holder.ivImg.setBackgroundResource(R.mipmap.img_upload_type4);
                holder.tvText1.setText("疫源疫病");
                holder.tvText2.setText("Foci disease");
                break;
        }


        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            try {
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    class ViewHolder {
        public LinearLayout layout;
        public ImageView ivImg;
        public TextView tvText1;
        public TextView tvText2;
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
